package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.user.BoxData;
import com.zhongxi.guoyuan.utils.CommonTool;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */

public class BoxAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<BoxData> list;
    private int type;

    public BoxAdapter(Context context, List<BoxData> data) {
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.list = data;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView == null){
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_box, parent, false);
                holder.linearBackground = (LinearLayout) convertView.findViewById(R.id.linearBackground);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.ivChange = (ImageView) convertView.findViewById(R.id.ivChange);
                holder.tvSource = (TextView) convertView.findViewById(R.id.tvSource);
                holder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
                holder.tvType = (TextView) convertView.findViewById(R.id.tvType);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            if(position%2==0){
                holder.linearBackground.setBackgroundColor(context.getResources().getColor(R.color.bg_box_one));
            }else{
                holder.linearBackground.setBackgroundColor(context.getResources().getColor(R.color.bg_box_two));
            }
            BoxData data = list.get(position);
            if(null!=data.getCtime()&&!"".equals(data.getCtime())){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = CommonTool.StringPattern(data.getCtime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd");
                holder.tvTime.setText(time);
            }else{
                holder.tvTime.setText("2016-12-06");
            }
            if(null!=data.getSourcedesc()&&!"".equals(data.getSourcedesc())){
                holder.tvSource.setText(data.getSourcedesc());
            }else{
                holder.tvSource.setText("");
            }
            String str = "";
            switch (type){
                case 1:
                    str = "美币";
                    break;
                case 2:
                    str = "元";
                    break;
                case 3:
                    str = "银币";
                    break;
            }
            if(null!=data.getNum()&&!"".equals(data.getNum())){
                holder.tvMoney.setText(data.getNum()+str);
            }else{
                holder.tvMoney.setText("*"+str);
            }
            if("1".equals(data.getUsetype())){
                holder.ivChange.setImageResource(R.mipmap.iv_box_add);
                holder.tvType.setText("获得");
            }else if("2".equals(data.getUsetype())){
                holder.ivChange.setImageResource(R.mipmap.iv_box_sub);
                holder.tvType.setText("消费");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder{
        private LinearLayout linearBackground;
        private TextView tvTime;
        private ImageView ivChange;
        private TextView tvSource;
        private TextView tvMoney;
        private TextView tvType;
    }
}
